package com.kj.tms.common.utils.calx.formula;


import com.kj.tms.common.utils.NumberUtils;
import com.kj.tms.common.utils.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ProjectName: ledger
 * @Package: com.hongtu.tms.ledger.calx.factory.formula
 * @ClassName: Statistic
 * @Description: [excel 统计函数]
 * @Author: [zhuyongjie]
 * @CreateDate: 2018/4/18 9:43
 * @UpdateUser: []
 * @UpdateDate: 2018/4/18 9:43
 * @UpdateRemark: []
 * @Version: 1.0
 * Copyright: 北京宏图天安 Copyright (c) 2018
 */
public class Statistic {
    private static Logger logger = LoggerFactory.getLogger(Statistic.class);
    /**
     * @Method      AVERAGE
     * @Param		rangeList
     * @Return      java.lang.Double
     * @Exception
     * @Description [excel average函数平均数]
     * @Author      zhuyongjie
     * @Version     1.0
     * @Date        2018/4/18 9:45
     */
    public static Double AVERAGE(List rangeList){
       int count = 0;
       Double result = null;
       double sigma = 0;
       for(int i = 0;i < rangeList.size();i ++){
           Object rangeObj = rangeList.get(i);
           if(rangeObj instanceof String){
               String v1 = StringUtil.toString(rangeObj);
               if(NumberUtils.isRealNumber(v1)){
                   double v2 = StringUtil.toDouble(v1);
                   sigma += v2;
                   count ++;
               }//if end
           }else if(rangeObj instanceof Integer){
               sigma += StringUtil.toDouble(rangeObj);
               count ++;
           }else if(rangeObj instanceof Double){
               sigma += StringUtil.toDouble(rangeObj);
               count ++;
           }else if(rangeObj instanceof HashMap){
               Map<String,Object> map = (HashMap) rangeObj;
               for(Map.Entry<String,Object> entry : map.entrySet()){
                   Object value = entry.getValue();
                   if(value instanceof String){
                       String v1 = StringUtil.toString(value);
                       if(NumberUtils.isRealNumber(v1)){
                           double v2 = StringUtil.toDouble(v1);
                           sigma += v2;
                           count ++;
                       }//if end
                   }else if(value instanceof Integer){
                       sigma += StringUtil.toDouble(value);
                       count ++;
                   }else if(value instanceof Double){
                       sigma += StringUtil.toDouble(value);
                       count ++;
                   }
               }//for end
           }//if end
       }//for end

       if(count != 0){
           result = sigma / count;
       }
//        System.out.println("Statistic.AVERAGE("+rangeList.toString()+") calculation   result : " + result);
        logger.info("Statistic.AVERAGE("+rangeList.toString()+") calculation   result : " + result);
       return result;
    }//AVERAGE end

    /**
     * @Method      COUNT
     * @Param		params
     * @Return      java.lang.Integer
     * @Exception
     * @Description [excel count()统计函数]
     * @Author      zhuyongjie
     * @Version     1.0
     * @Date        2018/4/18 10:06
     */
    public static Integer COUNT(Object params) {
        int result = 0;
        if(params instanceof ArrayList){
            //count(a1,a2,b3,b4)
            List countList = (ArrayList) params;
            for(Object obj : countList){
                if(obj instanceof HashMap){
                    result += ((HashMap)obj).size();
                }else{
                    result += 1;
                }
            }//for end
        }else if(params instanceof HashMap){
            //count(a1:b6)
            Map countMap = (Map) params;
            result += countMap.size();
        }else if(params instanceof String){
            //count(1)
            result += 1;
        }
//        System.out.println("Statistic.COUNT("+params.toString()+") calculation result : " + null);
        logger.info("Statistic.COUNT("+params.toString()+") calculation result : " + result);
        return result;
    }// COUNT end
}
